package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

func test(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "this is a https test for you")
}

func main2() {
	http.HandleFunc("/test", test)
	// err := http.ListenAndServeTLS(":8081", "/root/ca/cert.pem", "/root/ca/key.pem", nil)
	err := http.ListenAndServe(":8081", nil)
	if err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}

func main() {
	req, _ := http.NewRequest("GET", "https://api.github.com/users/helei112g", nil)
	// 这里设置了超时时间,context包和接口
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*1)
	// 取消函数
	defer cancel()
	req = req.WithContext(ctx)
	// 只给1s的时间
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatalln("request Err", err.Error())
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}
